package com.xxy.usercenter.controller;

import com.xxy.usercenter.common.BaseResponse;
import com.xxy.usercenter.common.DeleteRequest;
import com.xxy.usercenter.common.ErrorCode;
import com.xxy.usercenter.exception.BusinessException;
import com.xxy.usercenter.model.dto.UserLoginRequest;
import com.xxy.usercenter.model.dto.UserRegisterRequest;
import com.xxy.usercenter.model.vo.UserVO;
import com.xxy.usercenter.service.UserService;
import com.xxy.usercenter.utils.ResultUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: UserController
 * Package: com.xxy.usercenter.controller
 * Description: 用户请求Controller
 *
 * @Author 十一
 * @Create 2024/11/14 15:58
 * @Version 1.0
 */
@Tag(name = "用户管理")     // 描述controller类的作用
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册请求体
     * @return 新用户id
     */
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long result = userService.userRegister(userRegisterRequest);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录请求体
     * @param request          request请求对象
     * @return 当前登录用户
     */
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public BaseResponse<UserVO> userRegister(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        // 判断参数
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(userVO);
    }

    /**
     * 用户注销
     *
     * @param request request请求对象
     * @return 返回值大于0表示注销成功
     */
    @Operation(summary = "用户注销")
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        int result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 获取当前登录用户
     *
     * @param request request请求对象
     * @return 当前登录用户
     */
    @Operation(summary = "获取当前登录用户")
    @GetMapping("/current")
    public BaseResponse<UserVO> getCurrentUser(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getCurrentUser(request);
        return ResultUtils.success(userVO);
    }

    /**
     * 根据id删除用户
     *
     * @param deleteRequest 用户删除请求体
     * @param request       request请求对象
     * @return 返回值大于0表示删除成功
     */
    @Operation(summary = "根据id删除用户")
    @DeleteMapping("/delete")
    public BaseResponse<Integer> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        int result = userService.deleteUser(deleteRequest.getId(), request);
        return ResultUtils.success(result);
    }

    /**
     * 根据用户名查找用户 (仅管理员可查询)
     *
     * @param username 用户名
     * @param request  request请求对象
     * @return 符合要求的用户集合
     */
    @Operation(summary = "根据用户名查找用户 (仅管理员可查询)")
    @GetMapping("/search")
    public BaseResponse<List<UserVO>> searchUsers(String username, HttpServletRequest request) {
        List<UserVO> resultList = userService.searchUsers(username, request);
        return ResultUtils.success(resultList);
    }

}
